package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallBrandMapper;
import org.linlinjava.litemall.db.domain.LitemallBrand;
import org.linlinjava.litemall.db.domain.LitemallBrandExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author jie
 * @date 2019/7/20 -9:51
 */
@Service
public class LitemallBrandService {

    @Resource
    LitemallBrandMapper brandMapper;

    public List<LitemallBrand> selectbrandList(){
        return brandMapper.selectBrandList();
    }

    public List<LitemallBrand> getAllBrand(String id, String name, Integer page, Integer size, String sort, String order) {
        LitemallBrandExample example = new LitemallBrandExample();
        LitemallBrandExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        if (!StringUtils.isEmpty(page)&&!StringUtils.isEmpty(size)){
            PageHelper.startPage(page, size);
        }
        return brandMapper.selectByExample(example);
    }

   public void add(LitemallBrand brand){
       brand.setAddTime(new Date());
       brand.setUpdateTime(new Date());
       brandMapper.insertSelective(brand);
   }

   public int updateById(LitemallBrand brand){
        brand.setUpdateTime(new Date());
        return brandMapper.updateByPrimaryKeySelective(brand);
   }

   public int delById(Integer id){
        return brandMapper.logicalDeleteByPrimaryKey(id);
   }

    public List<LitemallBrand> query(Integer page, Integer size){
        LitemallBrandExample example = new LitemallBrandExample();
        PageHelper.startPage(page,size);
        return brandMapper.selectByExample(example);
    }

    public LitemallBrand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

}
